package org.jeecg.modules.backstageModule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.PriceSystemModule.entity.PriceInervalEntity;
import org.jeecg.modules.backstageModule.entity.CategoryTechnologyEntity;
import org.jeecg.modules.backstageModule.entity.DiscountEntity;
import org.jeecg.modules.backstageModule.entity.FabricGroupEntity;
import org.jeecg.modules.backstageModule.service.DiscountService;
import org.jeecg.modules.zonglzp.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/Discount")
public class DiscountController {


    @Autowired
    DiscountService service;

    /*
    查询客户折扣的列表
     */
    @GetMapping("/list")
    public Result<?> getCustDiscountList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String discountSystemName

    ) {
        Page<DiscountEntity> page = new Page<DiscountEntity>();
        IPage<DiscountEntity> pageList = service.getCustDiscountList(page, discountSystemName);
        return Result.ok(pageList);
    }


    /**
     * 查询客户折扣体系详情
     * @param
     * @return
     */
    @GetMapping("/getdiscountSystemdetail")
    public Result<?> getdiscountSystemdetail(
            @RequestParam(name = "id")  String id
    ){
        DiscountEntity entity=service.getdiscountSystemdetail(id);
        return Result.ok(entity);
    }




    /*
    设置某客户的折扣政策
     */
    @PostMapping("/setDiscount")
    public Result<?> setDiscount(
            @RequestBody DiscountEntity entity
    ) {
        try {
            if (entity.getDiscount() == null || "".equals(entity.getDiscount())) {
                return Result.error("请输入折扣率！");
            }

            //这里去往那个折扣表里插入折扣数据
            int count = service.setDiscount(entity.getUserId(), entity.getDiscount(), entity.getDiscountObject(), entity.getDiscountType());
            if (count > 0) {
                return Result.ok("新增成功");
            } else {
                return Result.error("新增失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败");
        }
    }


    /*
    查询客户列表然后根据去年消费金额分组
     */

    @GetMapping("/getCustxiaofeiList")
    public Result<?> DiscountZhengCeList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo
    ) {
        Page<DiscountEntity> page = new Page<DiscountEntity>();
        IPage<DiscountEntity> pageList = service.getCustxiaofeiList(page);
        return Result.ok(pageList);
    }

    /*
    新增折扣体系
     */
    @GetMapping("/adddiscountSystem")
    public Result<?> adddiscountSystem(
            DiscountEntity entity
    ) {
        int count = service.adddiscountSystem(entity);
        if (count > 0) {
            return Result.ok("新增成功");
        } else {
            return Result.error("新增失败");
        }
    }


    /**
     * 编辑折扣体系

     * @return
     */
    @GetMapping("/updatediscountSystem")
    public Result<?> updatediscountSystem(
          DiscountEntity entity
    ){
        int count = service.updatediscountSystem(entity);
        if (count > 0) {
            return Result.ok("编辑成功");
        } else {
            return Result.error("编辑失败");
        }
    }



    /**
     * 删除折扣体系
     * @param pageSize
     * @param pageNo
     * @param discountId
     * @param category
     * @param orderType
     * @return
     */

    @GetMapping("/deletediscountSystem")
    public Result<?> deletediscountSystem(
          @RequestParam(name = "id")  String id
    ){

        Page<DiscountEntity> page = new Page<DiscountEntity>(1, 10);
        IPage<DiscountEntity> pageList = service.getDiscountCustomers(page,null,null,id);
       if(pageList!=null&&pageList.getRecords().size()>0){
           return Result.error("该折扣体系下还有客户，请先清除客户后再删除");
       }
        int count = service.deletediscountSystem(id);
        if (count > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /*
    查询品类工艺折扣列表
     */
    @GetMapping("/SetdiscountList")
    public Result<?> SetdiscountList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "discountId") String discountId,
            @RequestParam(name = "category", defaultValue = "0") Integer category,
            @RequestParam(name = "orderType", defaultValue = "10") Integer orderType
    ) {
        Page<CategoryTechnologyEntity> page = new Page<CategoryTechnologyEntity>(pageNo, pageSize);
        IPage<CategoryTechnologyEntity> pageList = service.getSetdiscountList(page, discountId,category,orderType);

        return Result.ok(pageList);
    }

    /*
    查询面料折扣体系列表
     */
    @GetMapping("/mianliaodiscountsystemList")
    public Result<?> mianliaodiscountsystemList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "discountId") String discountId,
            String fabricDiscountSystemName
    ){
        Page<CategoryTechnologyEntity> page = new Page<CategoryTechnologyEntity>(pageNo, pageSize);
        IPage<CategoryTechnologyEntity> pageList=service.getmianliaodiscountsystemList(page,discountId,fabricDiscountSystemName);
        return Result.ok(pageList);
    }

    /*
    查询折扣用户列表
     */
    @GetMapping("/getDiscountCustomers")
    public Result<?> getDiscountCustomers(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String companyName,
            String companyCode,
            @RequestParam(name = "discountId") String discountId
    ) {
        Page<DiscountEntity> page = new Page<DiscountEntity>(pageNo, pageSize);
        IPage<DiscountEntity> pageList = service.getDiscountCustomers(page,companyName,companyCode,discountId);
        return Result.ok(pageList);
    }


    /*
    删除用户折扣
     */
    @GetMapping("/deleteCustomerDiscount")
    public Result<?> deleteCustomerDiscount(
            @RequestParam(name = "id") int id
    ) {

        int count = service.deleteCustomerDiscount(id);
        if (count > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /*
      设置用户折扣，将选中的添加到这个折扣中
     */
    @PostMapping("/addUsertoDiscount")
    public Result<?> addUsertoDiscount(
              @RequestBody HashMap<String,Object> map

    ) {
        try {
        List<Integer> ids=(List<Integer>) map.get("ids");
        String discountId=(String) map.get("discountId");
        if(ids==null||ids.size()==0)
            return Result.error("未选择用户!");
        for(int i=0;i<ids.size();i++){
            //将客户添加到这个折扣下
            int count = service.addUsertoDiscount(ids.get(i), discountId);
        }
        return Result.ok("添加成功");

        }catch (Exception e){
            e.printStackTrace();
            return Result.error("出错啦");
        }
    }


    /*
    设置这个折扣类别下  某个折扣类别下的小品类什么的折扣
     */
    @PostMapping("/shezhizhekou")
    public Result<?> shezhizhekou(
            @RequestBody HashMap<String,Object> map
    ){

        try {
            int count=service.shezhizhekou(map.get("id"),map.get("discountRatio"),map.get("discountId"));
            if(count>0){
                return Result.ok("设置成功");
            }else {
                return Result.ok("未修改");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("设置失败");
        }
    }


    /*
    查询所有的品类
     */
    @GetMapping("/getallcategory")
    public Result<?> getallcategory(){
        List<HashMap<String,Object>> categoryList=service.getallcategory();
        return Result.ok(categoryList);
    }


   /*
     查询这个这个面料折扣体系下的所有价格区间
    */
   @GetMapping("/mianliaojietidiscountlist")
   public Result<?> mianliaojietidiscountlist(
           @RequestParam(name = "fabirDiscountSystemId") String fabirDiscountSystemId
   ) {
       List<PriceInervalEntity> list = service.getmianliaojietidiscountlist(fabirDiscountSystemId);
       return Result.ok(list);
   }

    /*
     给这个面料折扣体系增加一条面料阶梯数据
     */
    @PostMapping("/addfabicDiscountjieti")
    public Result<?> addfabicDiscountjieti(
            @RequestBody PriceInervalEntity entity
    ){
         try {
             //判断一下这个面料阶梯的这个区间是否已经设置
             String oldjieti=service.getoldjieti(entity.getFabirDiscountSystemId(),entity.getMinNumbers(),entity.getMaxNumbers());
             if(oldjieti!=null){
                 return Result.error("区间已经设置,请更改数字区间");
             }
             int count = service.addfabicDiscountjieti(entity);
             return Result.ok("添加成功");
         }catch (Exception e){
             e.printStackTrace();
             return Result.ok("请重试");
         }
    }

    /*
    假删除这条面料折扣阶梯数据
     */
    @GetMapping("/deletemianliaojieti")
    public Result<?> deletemianliaojieti(
            @RequestParam(name = "id") int id
    ){
      try {
          int count = service.deletemianliaojieti(id);
          return Result.ok("删除成功");
      }catch (Exception e){
          e.printStackTrace();
          return Result.ok("删除失败");
      }
    }


    /*
    查询所有的面料组
     */
    @GetMapping("/getallmianliaogroup")
    public Result<?> getallmianliaogroup(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String groupName
    ){

     Page<FabricGroupEntity> page=new Page<FabricGroupEntity>(pageNo,pageSize);
     IPage<FabricGroupEntity> pageList=service.getallmianliaogroup(page,groupName);
     return  Result.ok(pageList);
    }

    /*
    将面料组添加到这个面料体系折扣中
     */
    @PostMapping("/addgroupstodiscountsystem")
    public Result<?> addgroupstodiscountsystem(

            @RequestBody HashMap<String,Object> map
    ){
            try {
                String  discountId= (String) map.get("discountId");
                System.out.println("打印折扣体系id看一看"+discountId);
                List<Integer> ids=(List<Integer>) map.get("ids");
                Integer fabirDiscountSystemId=(Integer) map.get("fabirDiscountSystemId");
                if(ids==null||ids.size()==0)
                    return Result.error("未选择用户!");
                for(int i=0;i<ids.size();i++){
                    //将客户添加到这个折扣下
                    int count = service.addgrouptodiscountsystem(ids.get(i), fabirDiscountSystemId,discountId);
                }
                return Result.ok("添加成功");

            }catch (Exception e){
                e.printStackTrace();
                return Result.error("出错啦");
            }
    }

    /*
    添加一个面料折扣体系
     */
    @PostMapping("/addmiantiaoDiscountSystem")
    public Result<?> addmiantiaoDiscountSystem(
            @RequestBody  CategoryTechnologyEntity entity
    ){
         try{
             int count = service.addmiantiaoDiscountSystem(entity);
             return Result.ok("添加成功");
         }catch (Exception e){
             e.printStackTrace();
             return Result.error("出错啦");
         }
    }

    /*
     删除面料折扣体系
     */
    @GetMapping("/deleteFabricDiscountSystem")
    public Result<?> deleteFabricDiscountSystem(
          @RequestParam(name = "id")  int id
    ){
        try{
            int count = service.deleteFabricDiscountSystembyId(id);
            return Result.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("出错啦");
        }
    }

    /**
     * 查询一个折扣体系下的面料组折扣
     */
    @GetMapping("/getfabricDiscountGroupbyDiscountId")
    public Result<?> getfabricDiscountGroupbyDiscountId(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "id")  String id,
            String groupName,
            String groupCode
    ){
        Page<DiscountEntity> page = new Page<DiscountEntity>(pageNo, pageSize);
        IPage<DiscountEntity> pageList = service.getfabricGroupDiscountbyDiscountId(page,id,groupName,groupCode);
        return Result.ok(pageList);
    }

    /**
     * 修改某折扣体系下的面料组折扣
     */
    @PostMapping("/updateFabricGroupDiscount")
    public Result<?> updateFabricGroupDiscount(
            @RequestBody HashMap<String,Object> map
    ){
        int count = service.updateFabricGroupDiscount(map.get("id"),map.get("discountRatio"));
        if(count>0){
            return Result.ok("修改成功");
        }else {
            return Result.ok("修改失败");
        }
    }
}
